// Invoked on the commit-msg git hook by yorkie.
// firtst node ./bin/install.js

// # 主要type
// feat:     增加新功能
// fix:      修复bug
// # 特殊type
// docs:     只改动了文档相关的内容
// style:    不影响代码含义的改动，例如去掉空格、改变缩进、增删分号
// build:    构造工具的或者外部依赖的改动，例如webpack，npm
// refactor: 代码重构时使用
// revert:   执行git revert打印的message

// # 暂不使用type
// test:     添加测试或者修改现有测试
// perf:     提高性能的改动
// ci:       与CI（持续集成服务）有关的改动
// chore:    不修改src或者test的其余修改，例如构建过程或辅助工具的变动

const chalk = require('chalk');
const msgPath = process.env.GIT_PARAMS;
const msg = require('fs').readFileSync(msgPath, 'utf-8').trim();

const releaseRE = /^v\d/;
const commitRE =
    /^(revert: )?(feat|fix|docs|dx|refactor|perf|test|workflow|build|ci|chore|types|wip|release|deps)(\(.+\))?: .{1,50}/;

if (!releaseRE.test(msg) && !commitRE.test(msg)) {
    console.log();
    console.error(
        `  ${chalk.bgRed.white(' ERROR ')} ${chalk.red(`invalid commit message format.`)}\n\n` +
            chalk.red(`  Proper commit message format is required for automated changelog generation. Examples:\n\n`) +
            `    ${chalk.green(`feat: add 'comments' option`)}\n` +
            `    ${chalk.green(`fix: handle events on blur (close #28)`)}\n\n` +
            chalk.red(`  See .github/commit-convention.md for more details.\n`),
    );
    process.exit(1);
}
